package com.celan.year2023.month05.day29;

import java.util.*;

public class NestedIterator implements Iterator<Integer> {

    Queue<Integer> queue = new LinkedList<>();

    public NestedIterator(List<NestedInteger> nestedList) {
        dfs(nestedList);
    }

    private void dfs(List<NestedInteger> list) {
        for (NestedInteger nested : list) {
            if (nested.isInteger()) {
                queue.add(nested.getInteger());
            } else {
                dfs(nested.getList());
            }
        }
    }

    @Override
    public Integer next() {
        return hasNext() ? queue.poll() : Integer.valueOf(-1);
    }

    @Override
    public boolean hasNext() {
        return !queue.isEmpty();
    }

}
